package com.java.es;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * @ClassNameEs_Client
 * @Description
 * @Author liufei
 * @Date2021/12/8 10:32
 * @Version V1.0
 **/
public class Es_Client_Doc_Update {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //修改数据
        UpdateRequest request=new UpdateRequest();
        request.index("user").id("1001");
        User user=new User();
        user.setName("张三");
        user.setSex("男");
        user.setAge(30);
        String jsonString = JSON.toJSONString(user);
        request.doc(XContentType.JSON,"sex","女");
        UpdateResponse update = esClient.update(request, RequestOptions.DEFAULT);
        System.out.println(update.getResult());
        //关闭es客户端
        esClient.close();
    }
}
